class WelcomeController < ApplicationController
  before_filter :login_required
  
  def index
    @history = Match.find(:all, :conditions => ["(user_id = ? or oponent_id = ?) and date < ?", current_user.id, current_user.id, Time.now])
    
    @match = Match.new
    
    @planned_matches = Match.find(:all, :conditions => ["(user_id = ? or oponent_id = ?) and date >= ?", current_user.id, current_user.id, Time.now])
    
    @board = Match.find_by_sql ["select m1.id, match_id, winner_id, looser_id, date, score, " +
                                  "if(score_a > score_b,  score_a, score_b) as bestscore, " + 
                                  "if(score_a <= score_b,  score_a, score_b) as worstscore from scores m1 " +
                                  "left join matches a on a.id = m1.match_id " +
                                  "where winner_id = ? and score = (select Max(m2.score) from " +
                                  "scores m2 where m2.winner_id = m1.winner_id)", current_user.id ]
                                  
    @ladder = LeagueResults.find(:all, :conditions => ["league_id = ?", 1])
    
  end

end
